package com.iwic.smartkidsdaycares.util;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DatabaseAdapter  {
private Context mContext;
private SQLiteDatabase database;
private DatabaseHelper dbHelper;

public final String ACTIVITIES_ID = "_id";
public final String ACTIVITIES_TITLE = "title";
public final String ACTIVITIES_DESCRIPTION = "description";
public final String ACTIVITIES_PERIOD = "period";
public final String ACTIVITIES_ACTION_TIME = "action_time";

public final String ACTIVITYLOGS_ID = "_id";
public final String ACTIVITYLOGS_CHILD_ID = "child_id";
public final String ACTIVITYLOGS_ACTIVITY_ID = "activity_id";
public final String ACTIVITYLOGS_ACTIVITY_TIME = "activity_time";
public final String ACTIVITYLOGS_NOTE = "note";

public final String CHILDTALLS_ID = "_id";
public final String CHILDTALLS_CHILD_ID = "child_id";
public final String CHILDTALLS_CURR_TALL = "curr_tall";
public final String CHILDTALLS_IDEAL_TALL = "ideal_tall";
public final String CHILDTALLS_NOTE = "note";

public final String CHILDWEIGHTS_ID = "_id";
public final String CHILDWEIGHTS_CHILD_ID = "child_id";
public final String CHILDWEIGHTS_DATE_RECORDED = "date_recorded";
public final String CHILDWEIGHTS_CURR_WEIGHT = "curr_weight";
public final String CHILDWEIGHTS_IDEAL_WEIGHT = "ideal_weight";
public final String CHILDWEIGHTS_NOTE = "note";

public final String CHILDRENS_ID = "_id";
public final String CHILDRENS_PARENT_ID = "parent_id";
public final String CHILDRENS_CHILD_NAME = "child_name";
public final String CHILDRENS_DOB = "dob";
public final String CHILDRENS_GENDER = "gender";
public final String CHILDRENS_BIRTH_WEIGHT = "birth_weight";
public final String CHILDRENS_BIRTH_TALL = "birth_tall";
public final String CHILDRENS_CREATEDON = "createdon";
public final String CHILDRENS_PICTURE = "picture";

public final String DOCTORS_ID = "_id";
public final String DOCTORS_DOCTOR_NAME = "doctor_name";
public final String DOCTORS_TELP_NO = "telp_no";
public final String DOCTORS_MOBILE_NO = "mobile_no";

public final String IDEALPHYSICS_ID = "_id";
public final String IDEALPHYSICS_AGE = "age";
public final String IDEALPHYSICS_W_IDEAL = "w_ideal";
public final String IDEALPHYSICS_W_BELOW = "w_below";
public final String IDEALPHYSICS_T_IDEAL = "t_ideal";
public final String IDEALPHYSICS_T_BELOW = "t_below";

public final String PARENTS_ID = "_id";
public final String PARENTS_PARENT_NAME = "parent_name";
public final String PARENTS_AGE = "age";
public final String PARENTS_GENDER = "gender";
public final String PARENTS_ACTIVE = "active";

public final String BOOKCATEGORIES_ID = "_id";
public final String BOOKCATEGORIES_CATEGORY_NAME = "category_name";
public final String BOOKCATEGORIES_DESCRIPTION = "description";
public final String BOOKCATEGORIES_ICON_IMAGE = "icon_image";

public final String BOOKREF_ID = "_id";
public final String BOOKREF_CATEGORY_ID = "category_id";
public final String BOOKREF_TITLE = "title";
public final String BOOKREF_CONTENT = "content";
public final String BOOKREF_BABY_MONTH = "baby_month";

public final String TALLLOGS_ID = "_id";
public final String TALLLOGS_CHILD_ID = "child_id";
public final String TALLLOGS_CURRENT_TALL = "current_tall";
public final String TALLLOGS_RECORD_TIME = "record_time";

public final String STORYCATEGORY_ID = "_id";
public final String STORYCATEGORY_CATEGORY = "category";

public final String STORYCONTENTS_ID = "_id";
public final String STORYCONTENTS_CATEGORY_ID = "category_id";
public final String STORYCONTENTS_JUDUL = "judul";
public final String STORYCONTENTS_KONTEN = "konten";
public final String STORYCONTENTS_SUMBER = "sumber";


private static final String ACTIVITIES = "activities";
private static final String ACTIVITYLOGS = "activity_logs";
private static final String CHILDTALLS = "child_talls";
private static final String CHILDWEIGHTS = "child_weights";
private static final String CHILDRENS = "childrens";
private static final String DOCTORS = "doctors";
private static final String IDEALPHYSICS = "ideal_physics";
private static final String PARENTS = "parents";
private static final String BOOKCATEGORIES = "book_categories";
private static final String BOOKREF = "book_ref";
private static final String TALLLOGS = "tall_logs";
private static final String STORYCATEGORY = "story_category";
private static final String STORYCONTENTS = "story_contents";

public DatabaseAdapter(Context context) {
   // TODO Auto-generated constructor stub
   this.mContext=context;
}
public DatabaseAdapter Open() throws SQLException {
	dbHelper=new DatabaseHelper(mContext);
	database = dbHelper.getWritableDatabase();
	return this;
}

public Cursor getAllActivities() {
return database.query(ACTIVITIES,new String[] {
	ACTIVITIES_ID,
	ACTIVITIES_TITLE,
	ACTIVITIES_DESCRIPTION,
	ACTIVITIES_PERIOD,
	ACTIVITIES_ACTION_TIME}
	,null,null,null,null,null);
}
public Cursor getAllActivityLogs() {
return database.query(ACTIVITYLOGS,new String[] {
	ACTIVITYLOGS_ID,
	ACTIVITYLOGS_CHILD_ID,
	ACTIVITYLOGS_ACTIVITY_ID,
	ACTIVITYLOGS_ACTIVITY_TIME,
	ACTIVITYLOGS_NOTE}
	,null,null,null,null,null);
}
public Cursor getAllChildTalls() {
return database.query(CHILDTALLS,new String[] {
	CHILDTALLS_ID,
	CHILDTALLS_CHILD_ID,
	CHILDTALLS_CURR_TALL,
	CHILDTALLS_IDEAL_TALL,
	CHILDTALLS_NOTE}
	,null,null,null,null,null);
}
public Cursor getAllChildWeights() {
return database.query(CHILDWEIGHTS,new String[] {
	CHILDWEIGHTS_ID,
	CHILDWEIGHTS_CHILD_ID,
	CHILDWEIGHTS_DATE_RECORDED,
	CHILDWEIGHTS_CURR_WEIGHT,
	CHILDWEIGHTS_IDEAL_WEIGHT,
	CHILDWEIGHTS_NOTE}
	,null,null,null,null,null);
}
public Cursor getAllChildrens() {
return database.query(CHILDRENS,new String[] {
	CHILDRENS_ID,
	CHILDRENS_PARENT_ID,
	CHILDRENS_CHILD_NAME,
	CHILDRENS_DOB,
	CHILDRENS_GENDER,
	CHILDRENS_BIRTH_WEIGHT,
	CHILDRENS_BIRTH_TALL,
	CHILDRENS_CREATEDON,
	CHILDRENS_PICTURE}
	,null,null,null,null,null);
}
public Cursor getAllDoctors() {
return database.query(DOCTORS,new String[] {
	DOCTORS_ID,
	DOCTORS_DOCTOR_NAME,
	DOCTORS_TELP_NO,
	DOCTORS_MOBILE_NO}
	,null,null,null,null,null);
}
public Cursor getAllIdealPhysics() {
return database.query(IDEALPHYSICS,new String[] {
	IDEALPHYSICS_ID,
	IDEALPHYSICS_AGE,
	IDEALPHYSICS_W_IDEAL,
	IDEALPHYSICS_W_BELOW,
	IDEALPHYSICS_T_IDEAL,
	IDEALPHYSICS_T_BELOW}
	,null,null,null,null,null);
}
public Cursor getAllParents() {
return database.query(PARENTS,new String[] {
	PARENTS_ID,
	PARENTS_PARENT_NAME,
	PARENTS_AGE,
	PARENTS_GENDER,
	PARENTS_ACTIVE}
	,null,null,null,null,null);
}
public Cursor getAllBookCategories() {
return database.query(BOOKCATEGORIES,new String[] {
	BOOKCATEGORIES_ID,
	BOOKCATEGORIES_CATEGORY_NAME,
	BOOKCATEGORIES_DESCRIPTION,
	BOOKCATEGORIES_ICON_IMAGE}
	,null,null,null,null,null);
}
public Cursor getAllBookRef() {
return database.query(BOOKREF,new String[] {
	BOOKREF_ID,
	BOOKREF_CATEGORY_ID,
	BOOKREF_TITLE,
	BOOKREF_CONTENT,
	BOOKREF_BABY_MONTH}
	,null,null,null,null,null);
}
public Cursor getAllTallLogs() {
return database.query(TALLLOGS,new String[] {
	TALLLOGS_ID,
	TALLLOGS_CHILD_ID,
	TALLLOGS_CURRENT_TALL,
	TALLLOGS_RECORD_TIME}
	,null,null,null,null,null);
}
public Cursor getAllStoryCategory() {
return database.query(STORYCATEGORY,new String[] {
	STORYCATEGORY_ID,
	STORYCATEGORY_CATEGORY}
	,null,null,null,null,null);
}
public Cursor getAllStoryContents() {
return database.query(STORYCONTENTS,new String[] {
	STORYCONTENTS_ID,
	STORYCONTENTS_CATEGORY_ID,
	STORYCONTENTS_JUDUL,
	STORYCONTENTS_KONTEN,
	STORYCONTENTS_SUMBER}
	,null,null,null,null,null);
}
public Cursor fetchActivities(long rowId) throws SQLException {
	Cursor mCursor= database.query(true,ACTIVITIES,new String[] {
	ACTIVITIES_ID,
	ACTIVITIES_TITLE,
	ACTIVITIES_DESCRIPTION,
	ACTIVITIES_PERIOD,
	ACTIVITIES_ACTION_TIME},
	ACTIVITIES_ID+"="+Long.toString(rowId),null,null,null,null,null);
	if (mCursor != null) {
	mCursor.moveToFirst();
	}
	return mCursor;
}
public Cursor fetchActivityLogs(long rowId) throws SQLException {
	Cursor mCursor= database.query(true,ACTIVITYLOGS,new String[] {
	ACTIVITYLOGS_ID,
	ACTIVITYLOGS_CHILD_ID,
	ACTIVITYLOGS_ACTIVITY_ID,
	ACTIVITYLOGS_ACTIVITY_TIME,
	ACTIVITYLOGS_NOTE},
	ACTIVITYLOGS_ID+"="+Long.toString(rowId),null,null,null,null,null);
	if (mCursor != null) {
	mCursor.moveToFirst();
	}
	return mCursor;
}
public Cursor fetchChildTalls(long rowId) throws SQLException {
	Cursor mCursor= database.query(true,CHILDTALLS,new String[] {
	CHILDTALLS_ID,
	CHILDTALLS_CHILD_ID,
	CHILDTALLS_CURR_TALL,
	CHILDTALLS_IDEAL_TALL,
	CHILDTALLS_NOTE},
	CHILDTALLS_ID+"="+Long.toString(rowId),null,null,null,null,null);
	if (mCursor != null) {
	mCursor.moveToFirst();
	}
	return mCursor;
}
public Cursor fetchChildWeights(long rowId) throws SQLException {
	Cursor mCursor= database.query(true,CHILDWEIGHTS,new String[] {
	CHILDWEIGHTS_ID,
	CHILDWEIGHTS_CHILD_ID,
	CHILDWEIGHTS_DATE_RECORDED,
	CHILDWEIGHTS_CURR_WEIGHT,
	CHILDWEIGHTS_IDEAL_WEIGHT,
	CHILDWEIGHTS_NOTE},
	CHILDWEIGHTS_ID+"="+Long.toString(rowId),null,null,null,null,null);
	if (mCursor != null) {
	mCursor.moveToFirst();
	}
	return mCursor;
}
public Cursor fetchChildrens(long rowId) throws SQLException {
	Cursor mCursor= database.query(true,CHILDRENS,new String[] {
	CHILDRENS_ID,
	CHILDRENS_PARENT_ID,
	CHILDRENS_CHILD_NAME,
	CHILDRENS_DOB,
	CHILDRENS_GENDER,
	CHILDRENS_BIRTH_WEIGHT,
	CHILDRENS_BIRTH_TALL,
	CHILDRENS_CREATEDON,
	CHILDRENS_PICTURE},
	CHILDRENS_ID+"="+Long.toString(rowId),null,null,null,null,null);
	if (mCursor != null) {
	mCursor.moveToFirst();
	}
	return mCursor;
}
public Cursor fetchDoctors(long rowId) throws SQLException {
	Cursor mCursor= database.query(true,DOCTORS,new String[] {
	DOCTORS_ID,
	DOCTORS_DOCTOR_NAME,
	DOCTORS_TELP_NO,
	DOCTORS_MOBILE_NO},
	DOCTORS_ID+"="+Long.toString(rowId),null,null,null,null,null);
	if (mCursor != null) {
	mCursor.moveToFirst();
	}
	return mCursor;
}
public Cursor fetchIdealPhysics(long rowId) throws SQLException {
	Cursor mCursor= database.query(true,IDEALPHYSICS,new String[] {
	IDEALPHYSICS_ID,
	IDEALPHYSICS_AGE,
	IDEALPHYSICS_W_IDEAL,
	IDEALPHYSICS_W_BELOW,
	IDEALPHYSICS_T_IDEAL,
	IDEALPHYSICS_T_BELOW},
	IDEALPHYSICS_ID+"="+Long.toString(rowId),null,null,null,null,null);
	if (mCursor != null) {
	mCursor.moveToFirst();
	}
	return mCursor;
}
public Cursor fetchParents(long rowId) throws SQLException {
	Cursor mCursor= database.query(true,PARENTS,new String[] {
	PARENTS_ID,
	PARENTS_PARENT_NAME,
	PARENTS_AGE,
	PARENTS_GENDER,
	PARENTS_ACTIVE},
	PARENTS_ID+"="+Long.toString(rowId),null,null,null,null,null);
	if (mCursor != null) {
	mCursor.moveToFirst();
	}
	return mCursor;
}
public Cursor fetchBookCategories(long rowId) throws SQLException {
	Cursor mCursor= database.query(true,BOOKCATEGORIES,new String[] {
	BOOKCATEGORIES_ID,
	BOOKCATEGORIES_CATEGORY_NAME,
	BOOKCATEGORIES_DESCRIPTION,
	BOOKCATEGORIES_ICON_IMAGE},
	BOOKCATEGORIES_ID+"="+Long.toString(rowId),null,null,null,null,null);
	if (mCursor != null) {
	mCursor.moveToFirst();
	}
	return mCursor;
}
public Cursor fetchBookRef(long rowId) throws SQLException {
	Cursor mCursor= database.query(true,BOOKREF,new String[] {
	BOOKREF_ID,
	BOOKREF_CATEGORY_ID,
	BOOKREF_TITLE,
	BOOKREF_CONTENT,
	BOOKREF_BABY_MONTH},
	BOOKREF_ID+"="+Long.toString(rowId),null,null,null,null,null);
	if (mCursor != null) {
	mCursor.moveToFirst();
	}
	return mCursor;
}
public Cursor fetchTallLogs(long rowId) throws SQLException {
	Cursor mCursor= database.query(true,TALLLOGS,new String[] {
	TALLLOGS_ID,
	TALLLOGS_CHILD_ID,
	TALLLOGS_CURRENT_TALL,
	TALLLOGS_RECORD_TIME},
	TALLLOGS_ID+"="+Long.toString(rowId),null,null,null,null,null);
	if (mCursor != null) {
	mCursor.moveToFirst();
	}
	return mCursor;
}
public Cursor fetchStoryCategory(long rowId) throws SQLException {
	Cursor mCursor= database.query(true,STORYCATEGORY,new String[] {
	STORYCATEGORY_ID,
	STORYCATEGORY_CATEGORY},
	STORYCATEGORY_ID+"="+Long.toString(rowId),null,null,null,null,null);
	if (mCursor != null) {
	mCursor.moveToFirst();
	}
	return mCursor;
}
public Cursor fetchStoryContents(long rowId) throws SQLException {
	Cursor mCursor= database.query(true,STORYCONTENTS,new String[] {
	STORYCONTENTS_ID,
	STORYCONTENTS_CATEGORY_ID,
	STORYCONTENTS_JUDUL,
	STORYCONTENTS_KONTEN,
	STORYCONTENTS_SUMBER},
	STORYCONTENTS_ID+"="+Long.toString(rowId),null,null,null,null,null);
	if (mCursor != null) {
	mCursor.moveToFirst();
	}
	return mCursor;
}

public void insertActivities(Integer id,String title,String description,String period,String actiontime){
ContentValues values = new ContentValues();
values.put(ACTIVITIES_ID,id);
values.put(ACTIVITIES_TITLE,title);
values.put(ACTIVITIES_DESCRIPTION,description);
values.put(ACTIVITIES_PERIOD,period);
values.put(ACTIVITIES_ACTION_TIME,actiontime);

database.insert(ACTIVITIES, null, values);
}
public void insertActivityLogs(Integer id,String childid,String activityid,String activitytime,String note){
ContentValues values = new ContentValues();
values.put(ACTIVITYLOGS_ID,id);
values.put(ACTIVITYLOGS_CHILD_ID,childid);
values.put(ACTIVITYLOGS_ACTIVITY_ID,activityid);
values.put(ACTIVITYLOGS_ACTIVITY_TIME,activitytime);
values.put(ACTIVITYLOGS_NOTE,note);

database.insert(ACTIVITYLOGS, null, values);
}
public void insertChildTalls(Integer id,String childid,String currtall,String idealtall,String note){
ContentValues values = new ContentValues();
values.put(CHILDTALLS_ID,id);
values.put(CHILDTALLS_CHILD_ID,childid);
values.put(CHILDTALLS_CURR_TALL,currtall);
values.put(CHILDTALLS_IDEAL_TALL,idealtall);
values.put(CHILDTALLS_NOTE,note);

database.insert(CHILDTALLS, null, values);
}
public void insertChildWeights(Integer id,String childid,String daterecorded,String currweight,String idealweight,String note){
ContentValues values = new ContentValues();
values.put(CHILDWEIGHTS_ID,id);
values.put(CHILDWEIGHTS_CHILD_ID,childid);
values.put(CHILDWEIGHTS_DATE_RECORDED,daterecorded);
values.put(CHILDWEIGHTS_CURR_WEIGHT,currweight);
values.put(CHILDWEIGHTS_IDEAL_WEIGHT,idealweight);
values.put(CHILDWEIGHTS_NOTE,note);

database.insert(CHILDWEIGHTS, null, values);
}
public void insertChildrens(Integer id,String parentid,String childname,String dob,String gender,String birthweight,String birthtall,String createdon,String picture){
ContentValues values = new ContentValues();
values.put(CHILDRENS_ID,id);
values.put(CHILDRENS_PARENT_ID,parentid);
values.put(CHILDRENS_CHILD_NAME,childname);
values.put(CHILDRENS_DOB,dob);
values.put(CHILDRENS_GENDER,gender);
values.put(CHILDRENS_BIRTH_WEIGHT,birthweight);
values.put(CHILDRENS_BIRTH_TALL,birthtall);
values.put(CHILDRENS_CREATEDON,createdon);
values.put(CHILDRENS_PICTURE,picture);

database.insert(CHILDRENS, null, values);
}
public void insertDoctors(Integer id,String doctorname,String telpno,String mobileno){
ContentValues values = new ContentValues();
values.put(DOCTORS_ID,id);
values.put(DOCTORS_DOCTOR_NAME,doctorname);
values.put(DOCTORS_TELP_NO,telpno);
values.put(DOCTORS_MOBILE_NO,mobileno);

database.insert(DOCTORS, null, values);
}
public void insertIdealPhysics(Integer id,int age,String wideal,String wbelow,String tideal,String tbelow){
ContentValues values = new ContentValues();
values.put(IDEALPHYSICS_ID,id);
values.put(IDEALPHYSICS_AGE,age);
values.put(IDEALPHYSICS_W_IDEAL,wideal);
values.put(IDEALPHYSICS_W_BELOW,wbelow);
values.put(IDEALPHYSICS_T_IDEAL,tideal);
values.put(IDEALPHYSICS_T_BELOW,tbelow);

database.insert(IDEALPHYSICS, null, values);
}
public void insertParents(Integer id,String parentname,String age,String gender,String active){
ContentValues values = new ContentValues();
values.put(PARENTS_ID,id);
values.put(PARENTS_PARENT_NAME,parentname);
values.put(PARENTS_AGE,age);
values.put(PARENTS_GENDER,gender);
values.put(PARENTS_ACTIVE,active);

database.insert(PARENTS, null, values);
}
public void insertBookCategories(Integer id,String categoryname,String description,byte[] iconimage){
ContentValues values = new ContentValues();
values.put(BOOKCATEGORIES_ID,id);
values.put(BOOKCATEGORIES_CATEGORY_NAME,categoryname);
values.put(BOOKCATEGORIES_DESCRIPTION,description);
values.put(BOOKCATEGORIES_ICON_IMAGE,iconimage);

database.insert(BOOKCATEGORIES, null, values);
}
public void insertBookRef(Integer id,int categoryid,String title,String content,int babymonth){
ContentValues values = new ContentValues();
values.put(BOOKREF_ID,id);
values.put(BOOKREF_CATEGORY_ID,categoryid);
values.put(BOOKREF_TITLE,title);
values.put(BOOKREF_CONTENT,content);
values.put(BOOKREF_BABY_MONTH,babymonth);

database.insert(BOOKREF, null, values);
}
public void insertTallLogs(Integer id,String childid,String currenttall,String recordtime){
ContentValues values = new ContentValues();
values.put(TALLLOGS_ID,id);
values.put(TALLLOGS_CHILD_ID,childid);
values.put(TALLLOGS_CURRENT_TALL,currenttall);
values.put(TALLLOGS_RECORD_TIME,recordtime);

database.insert(TALLLOGS, null, values);
}
public void insertStoryCategory(Integer id,String category){
ContentValues values = new ContentValues();
values.put(STORYCATEGORY_ID,id);
values.put(STORYCATEGORY_CATEGORY,category);

database.insert(STORYCATEGORY, null, values);
}
public void insertStoryContents(Integer id,String categoryid,String judul,String konten,String sumber){
ContentValues values = new ContentValues();
values.put(STORYCONTENTS_ID,id);
values.put(STORYCONTENTS_CATEGORY_ID,categoryid);
values.put(STORYCONTENTS_JUDUL,judul);
values.put(STORYCONTENTS_KONTEN,konten);
values.put(STORYCONTENTS_SUMBER,sumber);

database.insert(STORYCONTENTS, null, values);
}}
